import json

from excel_tool import Excel
from my_tool import StringUtil, DateUtil, ValidateUtil
from mysql_tool import MySql


def zhhf_balance_card():
    """
    珠海华发储值卡导入
    :return:
    """

    def get_membership_data(row: list):
        return row[2], DateUtil.get_begin_timestamp(row[3]), DateUtil.get_end_timestamp(row[4]), row[8], row[0]

    file_path = '/Users/jl/Documents/珠海华发/会员卡导入.xlsx'
    values = Excel.iter_excel_calamine(get_membership_data, None, 1, file_path)
    sql = 'INSERT INTO leaguer.zhhf_balance_card (mobile, timefrom, timeto, balance, card_type_code) VALUES (%s, %s, %s, %s, %s)'
    database = dict(host='devserver6', user='leaguer', password='leaguer@LLWH', db='leaguer', port=3306)
    MySql.insert(database, sql, values)


def external_detail():
    """
    外部码导入
    {"lineno": "1", "rankno": "38", "playDate": "2024-08-23", "playTime": "星期四 10:41", "areaCnName": "一层普通坐席", "ticketPrice": 0.01, "venueCnName": "歌剧院", "programCnName": "测试取票项目"}
    :return: 
    """""

    def get_external_data(row: list):
        uuid = 'ET' + StringUtil.get_random_string(14)
        play_date = row[5].strftime('%Y-%m-%d')
        play_time = DateUtil.get_analyze_date(row[5])['cn_week'] + ' ' + row[6].strftime('%H:%M:%S')[:-3]
        data_dict = dict(lineno=row[3][:-1], rankno=row[4][:-1], playDate=play_date, playTime=play_time,
                         areaCnName=row[2], ticketPrice=row[7], venueCnName=row[1], programCnName=row[0])
        data_json = json.dumps(data_dict, indent=4)
        return uuid, 1047, data_json, row[8], 0, 'Y', 2105

    def ignore_row(row: list):
        return not ValidateUtil.validate_phone_number(row[8])

    file_path = '/Users/jl/Documents/珠海华发/订单导入.xlsx'
    values = Excel.iter_excel_calamine(get_external_data, None, 1, file_path)
    print(values)
    sql = 'INSERT INTO theatre.external_detail (uuid, layout_id, data, mobile, print_num, status, company_id) VALUES (%s, %s, %s, %s, %s, %s, %s)'
    database = dict(host='devserver6', user='theatre', password='Theatre!GLSM2022', db='theatre', port=3306)
    MySql.insert(database, sql, values)


def external_detail_check():
    """
    外部码核销导入
    {"lineno": "1", "rankno": "38", "playDate": "2024-08-23", "areaCnName": "一层普通坐席", "ticketPrice": 0.01, "programCnName": "测试取票项目"}
    :return: 
    """""

    def get_external_data(row: list):
        play_date = row[3][0:10]
        play_time = DateUtil.get_analyze_date(DateUtil.parse_timestamp(row[3]))['cn_week'] + ' ' + row[3][11:16]
        data_dict = dict(lineno=row[7][:-1], rankno=row[8][:-1], playDate=play_date, playTime=play_time,
                         areaCnName=row[6], ticketPrice=row[9], programCnName=row[1])
        data_json = json.dumps(data_dict, indent=4)
        return data_json, row[10], 1, row[12]

    def ignore_row(row: list):
        return row[11] != '已打印'

    file_path = '/Users/jl/Documents/珠海华发/存量项目订单明细表2024-8-27.xlsx'
    values = Excel.iter_excel_calamine(get_external_data, ignore_row, 1, file_path)
    sql = 'INSERT INTO theatre.external_detail_check (data, mobile, print_num, print_time) VALUES (%s, %s, %s, %s)'
    database = dict(host='devserver6', user='theatre', password='Theatre!GLSM2022', db='theatre', port=3306)
    MySql.insert(database, sql, values)


if __name__ == '__main__':
    # zhhf_balance_card()
    # external_detail()
    external_detail_check()
